package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.mapper.OrdersettingMapper;
import com.itcast.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements  OrdersettingService{
    @Autowired
    private OrdersettingMapper ordersettingMapper;
    @Override
    public void add(List<String[]> readExcel) {
        //封装ordersetting对象
        List<OrderSetting> orderSettings = new ArrayList<>();
        for (String[] strings:readExcel){
            Date orderDate = new Date(strings[0]);
            int number = Integer.parseInt(strings[1]);
            OrderSetting orderSetting = new OrderSetting(orderDate, number);
            orderSettings.add(orderSetting);
        }
        for(OrderSetting orderSetting:orderSettings){
            findByMonth(orderSetting);
        }
    }
    //根据日期查找数据库中是否有相同日期的值
    public void findByMonth(OrderSetting orderSetting){
        long num = ordersettingMapper.findByMonth(orderSetting.getOrderDate());
        if (num>0){
            //执行修改操作
            ordersettingMapper.update(orderSetting);
        }else{
            //执行添加操作
            ordersettingMapper.add(orderSetting);
        }
    }
    //加载套餐数据
    @Override
    public List<Map> findAll(String  date) {
        String beginDate = date + "-1";
        String endDate = date + "-31";
        Map map = new HashMap<>();
        map.put("beginDate",beginDate);
        map.put("endDate",endDate);
        List<OrderSetting> orderSettings = ordersettingMapper.findAll(map);
        List<Map> list = new ArrayList<>();
        for(OrderSetting orderSetting:orderSettings){
            Map map1 = new HashMap<>();
            map1.put("date",orderSetting.getOrderDate().getDate());
            map1.put("number",orderSetting.getNumber());
            map1.put("reservations",orderSetting.getReservations());
            list.add(map1);
        }
        return list;
    }

    /**
     * 修改日期
     * @param orderSetting
     */
    @Override
    public void update(OrderSetting orderSetting) {
        long byMonth = ordersettingMapper.findByMonth(orderSetting.getOrderDate());
        System.out.println(byMonth);
        if (byMonth>0){
            //执行修改操作
            ordersettingMapper.update(orderSetting);
        }else{
            //执行添加操作
            ordersettingMapper.add(orderSetting);
        }
    }
}